package com.magicborrow.activity;

import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import com.android.volley.VolleyError;
import com.magicborrow.Constants;
import com.magicborrow.R;
import com.magicborrow.utils.MD5;
import com.magicborrow.utils.UserTools;
import com.magicborrow.utils.VolleyTool;
import org.json.JSONException;
import org.json.JSONObject;
import java.util.HashMap;
import java.util.Map;

/**
 * Created by River on 6/15/16.
 */
public class ApplyWithDrawActivity extends BaseActivity{

    private String amount;
    private TextView yue;
    private EditText account;
    private EditText jine;
    private EditText name;
    private EditText password;
    private Button withdraw;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.apply_withdraw_activity);
        amount = getIntent().getExtras().getString("amount");
        initView();
        initData();
    }

    private void initView(){
        yue = (TextView)findViewById(R.id.yue);
        yue.setText("零钱余额￥"+amount+"元");
        account = (EditText)findViewById(R.id.account);
        jine = (EditText)findViewById(R.id.jine);
        name = (EditText)findViewById(R.id.name);
        withdraw = (Button)findViewById(R.id.withdraw);
        password = (EditText)findViewById(R.id.password);
    }

    private void initData(){
        withdraw.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String qian = jine.getText().toString();
                String acc = account.getText().toString();
                String na = name.getText().toString();
                String pas = password.getText().toString();
                if("".equals(acc)){
                    showShortMsg("请输入支付宝账号");
                    return;
                }
                if("".equals(na)){
                    showShortMsg("请输入支付宝收款人姓名");
                    return;
                }
                if("".equals(pas)){
                    showShortMsg("请输入魔借登录密码");
                    return;
                }
                if("".equals(qian)){
                    showShortMsg("请输入提现金额");
                    return;
                }
                double a = Double.parseDouble(amount);
                double b = Double.parseDouble(qian);
                if(b > a){
                    showShortMsg("提现金额不能大于余额");
                    return;
                }
                String md = MD5.getMD5(MD5.getMD5(pas));
                Map<String,String> params = new HashMap<>();
                params.put("amount",qian);
                params.put("aliName",na);
                params.put("aliAccount",acc);
                params.put("channel","pay_ali");
                params.put("password",md);
                params.put("acc_token",UserTools.getUser(ApplyWithDrawActivity.this).getAcc_token());
                VolleyTool.post(Constants.WITHDRAW_URL, params, new VolleyTool.HTTPListener() {
                    @Override
                    public <T> void onResponse(T t, int requestCode) {
                        try {
                            JSONObject json = new JSONObject(t.toString());
                            int code = json.getInt("code");
                            String message = json.getString("message");
                            if(code >=0){
                                showShortMsg("申请提现成功,请等待后台工作人员处理");
                                finish();
                            }else{
                                showShortMsg(message);
                            }
                        } catch (JSONException e) {
                            e.printStackTrace();
                        }
                    }

                    @Override
                    public void onErrorResponse(VolleyError error, int requestCode) {
                        showShortMsg("服务器当前无法链接");
                    }
                },0,null);

            }
        });
    }

}
